//
//  BaseTableViewController.m
//  MyMicroBlog
//
//  Created by Apple on 16/9/21.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "BaseTableViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "BaseView.h"
#import "OAuthViewController.h"
#import "MainNaviViewController.h"
@interface BaseTableViewController ()

@end

@implementation BaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isLogin = NO;
}

-(void)loadView{
    if (_isLogin) {
        [super loadView];
    }else{
        //自定义视图
        _bView = [[BaseView alloc]init];
        [self setView:_bView];
        //执行Block
        __weak typeof(self) weakSelf = self;
        _bView.loginBlock = ^{
            NSLog(@"我是自定义视图内的Block");
            [weakSelf requestLoginOperation];
        };
        //未登录时添加导航栏按钮
        [self setupUI];
    }
}

-(UIBarButtonItem *)itemWithTitle:(NSString *)title andImageName:(NSString *)imageName andTarget:(id)target andAction:(SEL)action{
    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc]init];
    UIButton *btn = [[UIButton alloc]init];
    if (imageName != nil) {
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    [btn setTitle:title forState:UIControlStateNormal];
    //设置文字颜色
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    //文字大小
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btnItem.customView = btn;
    [btn sizeToFit];
    return btnItem;
}

#pragma mark -搭建界面
-(void)setupUI{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"登录" andImageName:nil andTarget:self andAction:@selector(loginAction)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"注册" andImageName:nil andTarget:self andAction:@selector(loginAction)];
}

-(void)loginAction{
    NSLog(@"我是顶部视图的按钮");
    [self requestLoginOperation];
}

//登录操作
-(void)requestLoginOperation{
    OAuthViewController *OAuthVC = [[OAuthViewController alloc]init];
    UINavigationController *navi = [[MainNaviViewController alloc]initWithRootViewController:OAuthVC];
    [self.navigationController presentViewController:navi animated:YES completion:nil];
}
@end
